import Vue from 'vue';
import Router from 'vue-router';
const _import = require('./_import_' + process.env.NODE_ENV);
/* layout */
import Layout from '../views/layout/Layout';
/* login */
const Login = _import('login/index');
const authRedirect = _import('login/authredirect');

/* error page */
const Err404 = _import('error/404');

const registerRouter = requireContext => {
    let rMap = [];
    requireContext.keys().forEach(comp => {
        let vueComp = requireContext(comp);
        let rList = vueComp.r || [];
        for (let i = 0, l = rList.length; i < l; i++) {
            rMap.push(rList[i]);
        }
    });
    return rMap;
};

Vue.use(Router);
export const constantRouterMap = [
	{ path: '/login', name: 'login', component: Login, hidden: true },
	{ path: '/authredirect', name: 'authredirect', component: authRedirect, hidden: true },
	{ path: '/404', name: 'n404', component: Err404, hidden: true },
	{
		path: '/',
		name: 'home',
		component: Layout,
		redirect: '/home/index',
		hidden: true
	},
	{
		path: '/home',
		name: 'mhome',
		component: Layout,
		redirect: '/home/index',
		noDropdown: true,
		icon: 'xinrenzhinan',
		children: [
			{ 
				path: 'index', 
				name: 'home_index', 
				component: _import('home/index'), 
				meta: { title: '首页' } 
			}
		]
	}
]
export const asyncRouterMap = [
	{
		path: '/',
		component: Layout,
		redirect: 'noredirect',
		meta: { title: '用户管理' },
		icon: 'wujiaoxing',
		children: registerRouter(require.context('./map/', false, /\.route.js$/))
	}
];

export const otherRoterMap = [
	{ path: '*', redirect: '/404', hidden: true }
];

export default new Router({
	//mode: 'history', //后端支持可开
	scrollBehavior: () => ({ y: 0 }),
	routes: constantRouterMap.concat(asyncRouterMap, otherRoterMap)
});